//
//  AddPointViewController.m
//  MagictoHire
//
//  Created by 锋 on 2017/12/15.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "AddPointViewController.h"
#import "SelectWorkKindViewController.h"

@interface AddPointViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) UITableView *addPointTableView;
@property (nonatomic, strong) UITextField *jobTextField;
@property (nonatomic, strong) UITextField *priceTextField;
@property (nonatomic, strong) NSString *workKindStr;
@property (nonatomic, strong) SelectWorkKindModel *selectWorkModel;
@end

@implementation AddPointViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.workKindStr = @"";
    [self setupUI];
    [self setupFillDataTableView];
    // Do any additional setup after loading the view.
}

- (void)setupUI {
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(onClickEnsureBtn)];
    [rightBtn setTintColor:[UIColor whiteColor]];
    [rightBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], UITextAttributeFont,nil] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightBtn;
    self.navigationController.navigationBar.translucent = NO;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    singleTap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:singleTap];
    if (_isManager) {
        self.navigationItem.title = @"管理者工价";
    } else {
        self.navigationItem.title = @"添加节点";
    }
    self.view.backgroundColor = RGB(242, 242, 242);
}
- (void)setupFillDataTableView {
    _addPointTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _addPointTableView.delegate = self;
    _addPointTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _addPointTableView.tableHeaderView = view;
    _addPointTableView.tableFooterView = view;
    _addPointTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_addPointTableView];
}

- (void)onClickEnsureBtn {
    WEAKSELF;
    if (!_isManager&&!_isAddPoint) {
        if ([_selectWorkModel.name isEqual:@""] || !_selectWorkModel || [_selectWorkModel isKindOfClass:[NSNull class]]) {
            [LTools showMBProgressWithText:@"请选择工种" addToView:weakSelf.view];
            return;
        }
    }
    if (_isAddPoint) {
        if (_jobTextField.text.length==0) {
            [LTools showMBProgressWithText:@"请输入节点名称" addToView:weakSelf.view];
            return;
        }
        if (_priceTextField.text.length==0) {
            [LTools showMBProgressWithText:@"请输入工作说明" addToView:weakSelf.view];
            return;
        }
    }
    if (_priceTextField.text.length==0) {
        [LTools showMBProgressWithText:@"请输入工价" addToView:weakSelf.view];
        return;
    }
    if (_isManager) {
        weakSelf.addPointBlock(_selectWorkModel, @"", _priceTextField.text);
    } else if (_isAddPoint) {
        SelectWorkKindModel *model = [[SelectWorkKindModel alloc] init];
        weakSelf.addPointBlock(model, _jobTextField.text, _priceTextField.text);
    } else {
        weakSelf.addPointBlock(_selectWorkModel, _jobTextField.text, _priceTextField.text);
    }
    [weakSelf.navigationController popViewControllerAnimated:YES];
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap {
    [self.view endEditing:YES];
}

// 我们为了防止tableView不能返回原始没有被点击的位置(这是防止用户在键盘弹出的时候，在滑动而造成的混乱)
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    /* 防止错乱停止编辑*/
    [self.view endEditing:YES];
}

// MARK:tableview的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_isManager) {
        return 2;
    } else if (_isAddPoint) {
        return 2;
    } else {
        return 3;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *ID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    if (!_isManager&&!_isAddPoint) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"选择工种";
            cell.detailTextLabel.text = _workKindStr;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else if (indexPath.row == 1) {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.text = @"工作说明";
            if (!_jobTextField) {
                _jobTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 0, SCREEN_WIDTH - 150, 44)];
                _jobTextField.backgroundColor = [UIColor clearColor];
                _jobTextField.delegate = self;
                _jobTextField.textAlignment = NSTextAlignmentLeft;
                _jobTextField.textColor = UIColorHexValue(0xB3B3B3);
                _jobTextField.placeholder = @"请输入工作说明";
                [cell.contentView addSubview:_jobTextField];
            }
        } else {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.text = @"工价";
            if (!_priceTextField) {
                _priceTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 0, SCREEN_WIDTH - 150, 44)];
                _priceTextField.backgroundColor = [UIColor clearColor];
                _priceTextField.delegate = self;
                _priceTextField.textAlignment = NSTextAlignmentLeft;
                _priceTextField.textColor = UIColorHexValue(0xB3B3B3);
                _priceTextField.placeholder = @"请输入工价";
                _priceTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                [cell.contentView addSubview:_priceTextField];
            }
        }
    } else if (_isAddPoint) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"节点名称";
            if (!_jobTextField) {
                _jobTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 0, SCREEN_WIDTH - 150, 44)];
                _jobTextField.backgroundColor = [UIColor clearColor];
                _jobTextField.delegate = self;
                _jobTextField.textAlignment = NSTextAlignmentLeft;
                _jobTextField.textColor = UIColorHexValue(0xB3B3B3);
                _jobTextField.placeholder = @"请输入节点名称";
                [cell.contentView addSubview:_jobTextField];
            }
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"工作说明";
            if (!_priceTextField) {
                _priceTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 0, SCREEN_WIDTH - 150, 44)];
                _priceTextField.backgroundColor = [UIColor clearColor];
                _priceTextField.delegate = self;
                _priceTextField.textAlignment = NSTextAlignmentLeft;
                _priceTextField.textColor = UIColorHexValue(0xB3B3B3);
                _priceTextField.placeholder = @"请输入工作说明";
                [cell.contentView addSubview:_priceTextField];
            }
        }
    } else {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"选择工种";
            cell.detailTextLabel.text = @"管理者";
            //            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else {
            cell.textLabel.text = @"工价";
            if (!_priceTextField) {
                _priceTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 0, SCREEN_WIDTH - 150, 44)];
                _priceTextField.backgroundColor = [UIColor clearColor];
                _priceTextField.delegate = self;
                _priceTextField.textAlignment = NSTextAlignmentLeft;
                _priceTextField.textColor = UIColorHexValue(0xB3B3B3);
                _priceTextField.placeholder = @"请输入工价";
                _priceTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                [cell.contentView addSubview:_priceTextField];
            }
            _priceTextField.text = _managerPrice;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.textLabel.textColor = RGB(77, 77, 77);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!_isManager && !_isAddPoint) {
        if (indexPath.row == 0) {
            SelectWorkKindViewController *selectWorkKindVC = [[SelectWorkKindViewController alloc] init];
            WEAKSELF;
            selectWorkKindVC.selectWorkKindBlock = ^(SelectWorkKindModel *workKindModel) {
                _selectWorkModel = workKindModel;
                weakSelf.workKindStr = workKindModel.name;
                [weakSelf.addPointTableView reloadData];
            };
            [self.navigationController pushViewController:selectWorkKindVC animated:YES];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
